package com.aim.analysis.records;

import java.util.LinkedList;

/**
 * User: Avraham Shvartzon
 * Date: 1/30/15
 */
public class MovingAverage implements PerformancePredictor<MovingAverage.Params> {

    private Params p;

    @Override
    public double[] predict(double[] performance) {
        double sum = 0;
        double[] prediction = new double[performance.length];

        LinkedList<Double> values = new LinkedList<Double>();
        for (int i = 0; i < performance.length; i++) {
                prediction[i] = i <= p.minSamples ? p.initialAverage : sum / (i + 1);
            sum += performance[i];
            values.add(performance[i]);
            if (values.size() > p.numOfSamples){
                sum -= values.removeFirst();
            }
        }
        return prediction;
    }

    @Override
    public void setParameters(Params parameters) {
        this.p = parameters;
    }

    public static class Params{

        final int numOfSamples;
        final double initialAverage;
        final double minSamples;

        public Params(int numOfSamples, double initialAverage, double minSamples) {
            this.numOfSamples = numOfSamples;
            this.initialAverage = initialAverage;
            this.minSamples = minSamples;
        }

        @Override
        public String toString() {
            return "numOfSamples=" + numOfSamples +
                    ", initialAverage=" + initialAverage +
                    ", minSamples=" + minSamples +
                    '}';
        }
    }
}
